//
//  KingGEAppDelegate.m
//  KingGE
//
//  Created by king on 10/4/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "KingGEAppDelegate.h"
#import "KingGEViewController.h"
#import "gsMain.h"

#define IPHONE_WIDTH 480
#define IPHONE_HEIGHT 320

@implementation KingGEAppDelegate

@synthesize window;
@synthesize viewController;


/*- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];

    return YES;
}*/

-(void) applicationDidFinishLaunching:(UIApplication *)application {
	[NSTimer scheduledTimerWithTimeInterval:0.033 target:self selector:@selector(gameLoop:) userInfo:nil repeats:NO];
	[self doStateChange:[gsMain class]];
}

-(void) gameLoop: (id) sender {
	double currTime = [[NSDate date] timeIntervalSince1970];
	m_FPS_framesThisSecond++;
	float timeThisSecond = currTime - m_FPS_lastSecondStart;
	if (timeThisSecond > 1.0f) {
		m_FPS = m_FPS_framesThisSecond;
		m_FPS_framesThisSecond = 0;
		m_FPS_lastSecondStart = currTime;
	}
	
	[((GameState*)viewController.view) Update];
	[((GameState*)viewController.view) Render];
	
	[NSTimer scheduledTimerWithTimeInterval:0.033 target:self selector:@selector(gameLoop:) userInfo:nil repeats:NO];
}

-(void) doStateChange:(Class)state {
	if (viewController.view != nil) {
		[viewController.view removeFromSuperview];
		[viewController.view release];
	}
	viewController.view = [[state alloc] initWithFrame:CGRectMake(0, 0, IPHONE_HEIGHT, IPHONE_WIDTH) andManager:self];
	[window addSubview:viewController.view];
	[window makeKeyAndVisible];
}


- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}

- (int) getFramesPerSecond {
	return m_FPS;
}


@end
